$(function () {


   var productTable = function(list) {
        var str = '';
        for (var i = 0; i < list.length; i++) {

            str += '<tr data-id="' + list[i].id + '">' +
                    '<td>' + list[i].title + '</td>' +
                    '<td>' + list[i].sort + '</td>' +
                    // '<td><img width="60px" src="' + list[i].cover + ' "/> </td>' +
                '<td>' +
                '<a href="#/product/typeedit/'+ list[i].id +'" class="btn btn-primary btn-xs" >编辑</a>  ' +
                '<a class="btn btn-danger btn-xs btn-delete" >删除</a> ' +
                '</td>' +
                    '</tr>'
        }

        return str
    }

    var productPage = new PageCreate({
        url:'/commodityType/doPageQuery',
        container:$("#table tbody"),
        pagination:$("#page"),
        success:function (container,data,body) {
            container.html(productTable(data.list));

            /**
             * 删除
             */
            $('.btn-delete').off('click').on('click',function(){
                var id = $(this).parents('tr').data('id');
                layer.confirm("确定进行此操作吗？", function (index) {
                    $.restPost('/commodityType/doRemove/' + id,{}).done(function(mess,data){
                        $.rootSuccessDialog(mess,1500);
                        layer.close(index)
                        init();
                    }).fail(function(mess){
                        $.rootErrorDialog(mess,1500);
                    })
                });

            });
        }
    });

    var searchPage =  function () {
        var $formQuery =$(".form-query");


        function queryDatum() {
            productPage.init({
                pageNum: 1,
                pageSize: 10,
                token: window.localStorage.getItem('token'),
                title:$formQuery.find('.form-control[name=title]').val() || ""
            });
        }

        function resetDatum() {
            $formQuery.find('.form-control[name=title]').val("")
            productPage.init({
                pageNum: 1,
                pageSize: 10,
                token: window.localStorage.getItem('token')
            })
        }

        $("#queryDatum").off("click").on("click", function () {
            queryDatum()
        });

        $("#resetDatum").off("click").on("click", function () {
            resetDatum()
        });
    }


    var init = function init() {

        productPage.init({
            pageNum: 1,
            pageSize: 10,
            token: window.localStorage.getItem('token')
        })

        searchPage()
    }


    init()
});
